package com.mydemo.springlearning.functions.paramreceive;


import com.alibaba.fastjson.JSON;
import com.mydemo.springlearning.functions.CRUDdemo.byxml.service.UserService;
import com.mydemo.springlearning.functions.paramreceive.entity.MyTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RequestMapping("param")
@RestController
public class ReceiveController {

    @Autowired
    private UserService userService;

    /**
     * Bean接收
     * get/post 都必须把参数放在URL中(Params)
     *
     * @param user user
     * @return java.lang.String
     * @author hwx
     * @date 2022/6/29 16:56
     */
    @RequestMapping("get1")
    public String get1(User user) {
        System.out.println(user);
        return "get ok";
    }

    /**
     * 从body获取参数 + @RequestBody
     *
     * @param user user
     * @return java.lang.String
     * @author hwx
     * @date 2022/6/29 17:07
     */
    @RequestMapping("get2")
    public String get2(@RequestBody User user) {
        System.out.println(user);
        return "get ok";
    }

    @RequestMapping("sleep")
    public String sleep() throws InterruptedException {
        System.out.println(Thread.currentThread().getName());
        Thread.sleep(50000L);
        return "sleep over";
    }

    @RequestMapping("task")
    public String myTask(@RequestBody List<MyTask> tasks) {
        return JSON.toJSONString(tasks);
    }

    @RequestMapping("get")
    public String get(@RequestBody Map obj) {
        System.out.println(obj);
        return "success";
    }


}
